﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace MedLink2011.Controls
{
    public static class FloatingWindowManager
    {
        private static readonly TimeSpan DefaultCloseDelay = new TimeSpan(0, 0, 0, 2);
        private static readonly TimeSpan DefaultOpenDelay = new TimeSpan(0, 0, 0, 1);

        public static int MaxX { get; set; }
        public static int MaxY { get; set; }

        private static bool isCurrentlyVisible;

        private static FloatableWindow _fWin = new FloatableWindow();
        public static FloatableWindow fWin
        {
            get { return _fWin; }
            set { _fWin = value; }
        }

        private static readonly DispatcherTimer Timer1 = new DispatcherTimer();
        private static readonly DispatcherTimer Timer2 = new DispatcherTimer();

        public static bool WillOpen = true;
        public static bool WillClose = true;

        public static bool hasBeenInitalized;

        public static FloatableWindow CreateNew()
        {
            var fWin2 = new FloatableWindow();
            return fWin2;
        }

        #region Show Logic

        public static void Show()
        {
            Show(DefaultOpenDelay);
        }

        public static void Show(TimeSpan ts)
        {
            if (!hasBeenInitalized)
            {
                hasBeenInitalized = true;
                fWin.MouseEnter += fWin_MouseEnter;
                fWin.MouseLeave += fWin_MouseLeave;

                Timer1.Tick += Show;
                Timer2.Tick += Close;
            }

            fWin.HorizontalOffset += 15;
            fWin.VerticalOffset += 15;

            if (fWin.HorizontalOffset < 0)
                fWin.HorizontalOffset = 0;

            if (fWin.VerticalOffset < 0)
                fWin.VerticalOffset = 0;

            Timer2.Stop();
            if (!isCurrentlyVisible)
            {
                //Timer1 = new DispatcherTimer();
                Timer1.Interval = ts;

                Timer1.Start();
            }
            else
            {
                //this is to let it redraw to new locations.
                fWin.Show();
            }
        }

        private static void Show(object sender, EventArgs e)
        {
            isCurrentlyVisible = true;
            Timer1.Stop();
            fWin.Show();
        }

        private static void StayShow()
        {
            Timer2.Stop();
        }

        #endregion

        # region Hide/Close Logic

        public static void Close()
        {
            Timer1.Stop();
            Close(DefaultCloseDelay);
        }

        public static void Close(TimeSpan ts)
        {
            //Timer2 = new DispatcherTimer();
            Timer2.Interval = ts;
            Timer2.Start();
        }

        public static void Close(object sender, EventArgs e)
        {
            Timer1.Stop();
            Timer2.Stop();

            if (isCurrentlyVisible)
            {
                isCurrentlyVisible = false;
                fWin.Close();
            }
        }

        public static void CloseNow()
        {
            fWin.Close();
        }

        #endregion

        # region Mouse over self behaviors

        private static void fWin_MouseLeave(object sender, MouseEventArgs e)
        {
            Close();
        }

        private static void fWin_MouseEnter(object sender, MouseEventArgs e)
        {
            StayShow();
        }

        # endregion
    }
}